package com.mall.mina.util;

import com.mall.mina.config.ResultUtil;
import org.springframework.util.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class AgreementUtil {

    /**
     * 拆卸出设备id
     * @param args 操作码（1个字节） + 负载（其中设备id为4个字节）
     */
    public static byte[] disassembleDeviceId(byte[] args){
        if (args == null || args.length < 5){
            throw new IllegalArgumentException("参数格式不正确！");
        }
        byte[] deviceIdBytes = new byte[]{args[1],args[2],args[3],args[4]};
        return deviceIdBytes;
    }

    /**
     * 对参数进行格式校验
     * @param args      参数列表
     */
    public static boolean checkAgreement(Object... args){
        for (Object arg:args){
            if (arg instanceof Integer){
                if (arg == null){
                    return true;
                }
            }

            if (arg instanceof String){
                if (arg == null || ((String) arg).replace(" ","").equals("")){
                    return true;
                }
            }

            if (arg instanceof Date){
                if (arg == null){
                    return true;
                }
            }
        }
        return false;
    }

    //将数组2拼接到数组1后面，形成新数组返回
    public static byte[] assembleAgreement(byte[] args1,byte[] args2){
        if (args1 == null || args1.length <= 0){
            return args2;
        }
        if (args2 == null || args2.length <= 0){
            return args1;
        }
        int len = args1.length + args2.length;
        byte[] bytes = new byte[len];
        for (int i = 0,j = 0,k = 0; i < bytes.length; i++,j++) {
            if (i < args1.length){
                bytes[i] = args1[j];
            }
            if (i >= args1.length){
                bytes[i] = args2[k];
                k++;
            }
        }
        return bytes;
    }

    //将short值转换成字节数组
    public static byte[] shortToByteArray(short s) {
        byte[] shortBuf = new byte[2];
        for(int i=0;i<2;i++) {
            int offset = (shortBuf.length - 1 -i)*8;
            shortBuf[i] = (byte)((s>>>offset)&0xff);
        }
        return shortBuf;
    }

    //将字节数组转换成short
    public static short byteArrayToShort(byte [] b) {
        if (b == null || b.length != 2){
            throw new IllegalArgumentException("参数异常");
        }
        return (short) ((b[0] << 8) + (b[1] & 0xFF));
    }

    //将int转换成字节数组
    public static byte[] intToByteArray(int value) {
        byte[] src = new byte[4];
        src[0] = (byte) ((value>>24) & 0xFF);
        src[1] = (byte) ((value>>16) & 0xFF);
        src[2] = (byte) ((value>>8) & 0xFF);
        src[3] = (byte) (value & 0xFF);
        return src;
    }
    //将字节数组转换成int
    public static int byteArrayToInt(byte [] b) {
        if (b == null || b.length != 4){
            throw new IllegalArgumentException("参数异常");
        }
        return (b[0] << 24) + ((b[1] & 0xFF) << 16) + ((b[2] & 0xFF) << 8) + (b[3] & 0xFF);
    }

    //将时间Date转换成字节数组
    public static byte[] dateToByteArray(Date date){
        if (date == null){
            throw new IllegalArgumentException("参数异常");
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yy MM dd HH mm ss");
        String format = sdf.format(date);
        String[] split = format.split(" ");

        String str = "";
        for (int i = 0; i < split.length; i++) {
            str = str + split[i];
        }

        byte[] bytes = hexStringToBytes(str);
        return bytes;
    }
    //将时间字节数组转换成Date
    public static Date dateByteArrayToDate(byte[] date) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyMMddHHmmss");
        if (date == null || date.length != 6){
            throw new IllegalArgumentException("参数异常");
        }
        String hexString = byteArrayToHexString(date);
        Date parse = sdf.parse(hexString);
        return parse;
    }

    //将byte数组转换成16进制字符串
    public static String byteArrayToHexString(byte[] b) {
        if (b == null || b.length < 1){
            throw new IllegalArgumentException("参数异常");
        }
        String str = "";
        for (int i = 0; i < b.length; i++) {
            String hex = Integer.toHexString(b[i] & 0xFF);
            if (hex.length() == 1) {
                hex = '0' + hex;
            }
            str = str + hex.toUpperCase();
        }
        return str;
    }
    private static byte charToByte(char c) {
        return (byte) "0123456789ABCDEF".indexOf(c);
    }
    //将16进制字符串转换成byte数组
    public static byte[] hexStringToBytes(String hexString) {
        if (hexString == null || hexString.replace(" ","").equals("")) {
            throw new IllegalArgumentException("参数异常");
        }
        hexString = hexString.replace(" ","").toUpperCase();
        int length = hexString.length() / 2;
        char[] hexChars = hexString.toCharArray();
        byte[] d = new byte[length];
        for (int i = 0; i < length; i++) {
            int pos = i * 2;
            d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
        }
        return d;
    }

    //将16进制数组转换成二进制字符串
    public static String byteArrayToBinaryString(byte[] arg){
        if (arg == null || arg.length < 1){
            throw new IllegalArgumentException("参数异常");
        }
        byte[] bytes = new byte[4];
        for (int i = bytes.length - 1,j = arg.length - 1; j >= 0; i--,j--) {
            bytes[i] = arg[j];
        }
        int i = byteArrayToInt(bytes);
        String binaryString = Integer.toBinaryString(i);

        //锁功能信息为二进制12个长度
        if (binaryString.length() < 12){
            String s = "";
            for (int j = 0; j < 12 - binaryString.length(); j++) {
                s = s + "0";
            }
            binaryString = s + binaryString;
        }
        return binaryString;
    }

    public static Integer byteToInt(byte arg){
        byte[] bRefArr = new byte[]{arg};
        int iOutcome = 0;
        byte bLoop;
        for (int i = 0; i < bRefArr.length; i++) {
            bLoop = bRefArr[i];
            iOutcome += (bLoop & 0xFF) << (8 * i);
        }
        return iOutcome;
    }

    public static boolean checkLock(Integer lockId){
        if(lockId==null || lockId<1 || lockId> ResultUtil.LOCK_NUM ){
            return true;
        }
        return false;
    }

    public static boolean checkAccountId(String accountId){
        try {
            Integer id = Integer.valueOf(StringUtils.hasLength(accountId)?accountId:"0");
            if(id<1 || id>ResultUtil.ACCOUNT_NUM ){
                return true;
            }
        } catch (Exception e){
            return true;
        }
        return false;
    }

    public static String changePass(String password) {
        String pass="";
        for(String str:password.split("")){
            pass+="0"+str;
        }
        return pass;
    }
}